<?php

class ModelCfNews extends Model {
        
	public function getItems($data = array()) {
        $sql = sprintf('SELECT * FROM %snews n LEFT JOIN %snews_description nd ON n.news_id = nd.news_id ' . 
                       'WHERE nd.language_id = %s ', 
                       DB_PREFIX, DB_PREFIX, (int)$this->config->get('config_language_id'));
		
		$sort_data = array('title', 'date_added');	
        $sql .= ' ORDER BY ' . ( isset($data['sort']) ? $data['sort'] : 'date_added' );		    
		$sql .= ( isset($data['order']) && ($data['order'] == 'DESC') ) ? ' DESC' : ' ASC';
		        
        $data['start'] = isset($data['start']) ? (int)$data['start'] : 0;  
        $data['limit'] = (int)($data['limit']) < 1 ? 20 : (int)$data['limit'];  
        
        $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
																																							  
		$query = $this->db->query($sql);																																				
		
		return $query->rows;	
	}
	
	/**
	 * 
	 * Enter description here...
	 * @return unknown_type
	 */
	public function getOlderNews($cur_news_id, $limit = 5)
	{
	    $sql = 'SELECT n.news_id, n.date_added, nd.title FROM ' . DB_PREFIX . 'news n ' . 
	           'LEFT JOIN ' . DB_PREFIX . 'news_description nd ON n.news_id = nd.news_id ' . 
               'WHERE nd.language_id = ' . (int)$this->config->get('config_language_id') . " AND n.news_id > {$cur_news_id} " . 
	           'LIMIT ' . ( (int)$limit > 0 ? (int)$limit : 5 ); 
	    
	    $query = $this->db->query($sql);
	    return $query->rows;
	}
	
    public function getNewsDetail($news_id)
    {
        $news_detail_data = array();
        
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_description WHERE news_id = " . (int)$news_id);
        
        foreach ($query->rows as $result) {
            $news_detail_data[$result['language_id']] = array(
                'title'         => $result['title'],
                'description'   => $result['description'],
                'content'       => $result['content']
            );
        }
        
        return $news_detail_data;
    }
    
	public function getTotalNews() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "news");
		
		return $query->row['total'];
	}
	
}
